/**
 * EstudiantesTest.java
 */

 /**
 *  AdSec: Administrador de Secundarias
 *  Copyright (C) 2009 Daniela Ramírez Chaves (dannyrch@gmail.com)
 *  Copyright (C) 2009 Leonel Salazar Valverde (leo87sv@gmail.com)
 *
 *  This file is part of AdSec.
 *  AdSec is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  AdSec is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with AdSec.  If not, see <http://www.gnu.org/licenses/>.
 */

package stresstest;
import java.util.LinkedList;
import java.util.Random;
import adsec.entidades.Estudiante;
import adsec.negocios.ControladoraEstudiante;
import adsec.negocios.UtilidadesCapaNegocios;


public class EstudiantesTest {

    private String raizCedulas = "9-0000-";
    private String [] poolNombres = {"María", "Daniela", "Esteban", "Leo",
        "Carolina","Francisco", "Vera", "Hernán", "Karla", "Carlos", "Viviana", "David",
        "Jennifer", "Wilberth", "Mariela", "Rodolfo", "Paola", "Juan José",
        "Melissa", "Alonso", "Nancy", "Roy", "Michelle", "Alvaro", "Sandra", "Vladimir",
        "Javier", "Marcela", "Monica", "Marianela", "Ana", "Marta", "Guillermo",
        "Jose", "Luis", "Diego", "Armando", "Ricardo", "Katia", "Carmen", "Kirma",
        "Alicia", "Silvia", "Alexander", "Alejandro"};
    private String [] poolApellidos = {"Ramirez", "Chaves", "Salazar", "Valverde",
        "Miranda", "Rojas", "Chacon", "Salas", "Delgado", "Rodriguez", "Barquero",
        "Solis", "Arias", "Mora", "Matteucci", "Araya", "Bermudez", "Solano", "Gang",
        "Quesada", "Lara", "Barrantes", "Marin", "Vasquez", "Kikut", "Pujol",
        "Sanabria"};
    private final int cantidad = 2000;
    private ControladoraEstudiante controladora;

    public EstudiantesTest(){

    }

    public static void main(String []args){
        
        EstudiantesTest test = new EstudiantesTest();
        //test.correrPruebas();
        //test.pruebaInsertarYConsultar();
        test.pruebaBorraryConsultar();
    }//fin de main

    public void correrPruebas(){
        long inicio = System.currentTimeMillis();
        this.pruebaInsertar();
        this.pruebaConsultar();
        this.pruebaBorrar();
        this.pruebaConsultar();
        long fin = System.currentTimeMillis();
        System.out.println("Milis en todas las pruebas: "+ (fin - inicio));
    }

    public void pruebaInsertarYConsultar(){
        long inicio = System.currentTimeMillis();
        this.pruebaInsertar();
        this.pruebaConsultar();
        long fin = System.currentTimeMillis();
        System.out.println("Milis en todas las pruebas: "+ (fin - inicio));
    }

    public void pruebaBorraryConsultar(){
        long inicio = System.currentTimeMillis();
        this.pruebaBorrar();
        this.pruebaConsultar();
        long fin = System.currentTimeMillis();
        System.out.println("Milis en todas las pruebas: "+ (fin - inicio));
    }

    public void pruebaInsertar(){
        controladora = new ControladoraEstudiante();
        int errores = 0;
        int resultado;
        long inicio = System.currentTimeMillis();
        for(int indice = 0; indice < cantidad; indice++){
            Estudiante estudiante = this.crearEstudiante(indice);
            resultado = controladora.insertarEstudiante(estudiante);
            if(resultado != UtilidadesCapaNegocios.QUERY_OK){
                errores++;
            }
        }
        long fin = System.currentTimeMillis();
        System.out.println("Milis insertando "+ cantidad + " : "+ (fin - inicio));
        System.out.println("Cantidad de errores " + errores);
    }//fin de pruebaInsertar

    public void pruebaBorrar(){
        controladora = new ControladoraEstudiante();
        int errores = 0;
        int resultado;
        long inicio = System.currentTimeMillis();
        for(int indice = 0; indice < cantidad; indice++){
            String cedula = this.crearCedula(indice);
            resultado = controladora.eliminarEstudiante(cedula);
            if(resultado != UtilidadesCapaNegocios.QUERY_OK){
                errores++;
            }
        }
        long fin = System.currentTimeMillis();
        System.out.println("Milis borrando "+ cantidad + " : "+ (fin - inicio));
        System.out.println("Cantidad de errores " + errores);
    }//fin de pruebaBorrar

    public void pruebaConsultar(){
        controladora = new ControladoraEstudiante();
        long inicio = System.currentTimeMillis();
        LinkedList<Estudiante> lista = controladora.consultarEstudiantes();
        long fin = System.currentTimeMillis();
        System.out.println("Milis consultando "+ cantidad + " : "+ (fin - inicio));
        System.out.println("Estudiantes consultados: "+lista.size());
    }//fin de pruebaConsultar


    public Estudiante crearEstudiante(int num){
        Estudiante estudiante = new Estudiante();
        estudiante.setCedula(this.crearCedula(num));
        Random random = new Random();
        int randomNombre = random.nextInt(this.poolNombres.length);
        int randomAp1 = random.nextInt(this.poolApellidos.length);
        int randomAp2 = random.nextInt(this.poolApellidos.length);
        estudiante.setNombre(poolNombres[randomNombre]);
        estudiante.setApellidos(poolApellidos[randomAp1] + " " + poolApellidos[randomAp2]);
        return estudiante;
    }

    public String crearCedula(int num){
        String numCed = Integer.toString(num);
        String sufijoCed = "";
        for(int i = 0; i < (4 - numCed.length()); i++){
            sufijoCed = sufijoCed + "0";
        }
        return raizCedulas + sufijoCed + numCed;
    }//fin de crearCedula

    
}//fin de EstudiantesTest
